package com.thx.tags;

import com.opensymphony.xwork2.util.ValueStack;
import com.thx.common.util.AppContext;
import com.thx.common.web.WebConstant;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.components.Component;
import org.apache.struts2.views.jsp.ComponentTagSupport;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义分页标签结构或属性.
 * 
 * @author 牛慧敏
 */
public class PagesTag extends ComponentTagSupport {
  
  private String pageNo;
  private String total;
  private String styleClass;
  private String theme;
  private String url;
  private String urlType;
  private String hasPerPage;// --是否有每页几条的选项
  private String hasGoto;// 是否有跳转

  public void setUrlType(String urlType) {
    this.urlType = urlType;
  }

  public void setUrl(String url) {
    this.url = url;
  }

  public void setTheme(String theme) {
    this.theme = theme;
  }

  public void setStyleClass(String styleClass) {
    this.styleClass = styleClass;
  }

  public void setPageNo(String pageNo) {
    this.pageNo = pageNo;
  }

  public void setTotal(String total) {
    this.total = total;
  }

  public void setHasPerPage(String hasPerPage) {
    this.hasPerPage = hasPerPage;
  }

  public void setHasGoto(String hasGoto) {
    this.hasGoto = hasGoto;
  }

  @Override
  public Component getBean(ValueStack arg0, HttpServletRequest arg1, HttpServletResponse arg2) {
    return new Pages(arg0, arg1);
  }

  protected void populateParams() {
    super.populateParams();
    Pages pages = (Pages) component;
    pages.setPageNo("page.curPage");
    pages.setTotal("page.totalPage");
    if (StringUtils.isBlank(styleClass)) {
      styleClass = AppContext.getProperties(WebConstant.STYLE_PAGE_KEY);
      if (StringUtils.isBlank(styleClass)) {
        styleClass = "themeText";
      }
    }
    pages.setStyleClass(styleClass);
    if (StringUtils.isBlank(theme)) {
      theme = AppContext.getProperties(WebConstant.THEME_PAGE_KEY);
      if (StringUtils.isBlank(theme)) {
        theme = "text";
      }
    }
    pages.setTheme(theme);
    pages.setUrl(url);
    pages.setUrlType(urlType);
    if (StringUtils.isBlank(hasGoto)) {
      hasGoto = AppContext.getProperties(WebConstant.HASGOTO_PAGE_KEY);
      if (StringUtils.isBlank(hasGoto)) {
        hasGoto = "true";
      }
    }
    pages.setHasGoto(Boolean.parseBoolean(hasGoto));
    if (StringUtils.isBlank(hasPerPage)) {
      hasPerPage = AppContext.getProperties(WebConstant.HASPER_PAGE_KEY);
      if (StringUtils.isBlank(hasPerPage)) {
        hasPerPage = "true";
      }
    }
    pages.setHasPerPage(Boolean.parseBoolean(hasPerPage));
  }
}
